package com.platform.api;

import com.github.pagehelper.PageHelper;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "首页接口文档")
@RestController
@RequestMapping("/api/index")
public class ApiIndexController extends ApiBaseAction {
    @Autowired
    private ApiAdService adService;
    @Autowired
    private ApiChannelService channelService;
    @Autowired
    private ApiGoodsService goodsService;
    @Autowired
    private ApiBrandService brandService;
    @Autowired
    private ApiTopicService topicService;
    @Autowired
    private ApiCategoryService categoryService;
    @Autowired
    private ApiCartService cartService;

    /**
     * 测试
     */
    @IgnoreAuth
    @PostMapping(value = "test")
    public Object test() {
        return toResponsMsgSuccess("请求成功yyy");
    }

    /**
     * app首页
     */
    @ApiOperation(value = "首页")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
            @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "index")
    public Object index(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        //param.put("ad_position_id", 1);
        List<AdVo> banner = adService.queryBannerList(deptId);
        resultObj.put("banner", banner);
        //
        //param = new HashMap<String, Object>();
        //param.put("sidx", "sort_order ");
        //param.put("order", "asc ");
        List<ChannelVo> channel = channelService.queryChannelList(deptId, null,null);
        resultObj.put("channel", channel);

//        param = new HashMap<String, Object>();
//        param.put("is_new", 1);
//        param.put("is_delete", 0);
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("root_dept_id", deptId);
//
//        param.put("fields", "id, name, list_pic_url, retail_price");
//        PageHelper.startPage(0, 4, false);
        List<GoodsVo> newGoods = goodsService.queryNewGoodsList(deptId, grade, null, null);
        resultObj.put("newGoodsList", newGoods);
        //
//        param = new HashMap<String, Object>();
//        param.put("is_hot", "1");
//        param.put("is_delete", 0);
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("root_dept_id", deptId);
//        PageHelper.startPage(0, 3, false);
        List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(deptId, grade, null,null);
        resultObj.put("hotGoodsList", hotGoods);
        // 当前购物车中
        List<CartVo> cartList = new ArrayList<CartVo>();
        if (null != getUserId()) {
            //查询列表数据
            Map<String, Object> cartParam = new HashMap<String, Object>();
            cartParam.put("user_id", getUserId());
            cartList = cartService.queryCartList();
        }
        if (null != cartList && cartList.size() > 0 && null != hotGoods && hotGoods.size() > 0) {
            for (GoodsVo goodsVo : hotGoods) {
                for (CartVo cartVo : cartList) {
                    if (goodsVo.getId().equals(cartVo.getGoods_id())) {
                        goodsVo.setCart_num(cartVo.getNumber());
                    }
                }
            }
        }
        //
//        param = new HashMap<String, Object>();
//        param.put("is_new", 1);
//        param.put("sidx", "new_sort_order ");
//        param.put("order", "asc ");
//        param.put("offset", 0);
//        param.put("limit", 4);
        List<BrandVo> brandList = brandService.queryBrandList(deptId, 0, 4);
        resultObj.put("brandList", brandList);

//        param = new HashMap<String, Object>();
//        param.put("offset", 0);
//        param.put("limit", 3);
        List<TopicVo> topicList = topicService.queryTopicList();
        resultObj.put("topicList", topicList);

//        param = new HashMap<String, Object>();
//        param.put("parent_id", 0);
//        param.put("notName", "推荐");//<>
        List<CategoryVo> categoryList = categoryService.queryCategoryList(null, null);
        List<Map<String, Object>> newCategoryList = new ArrayList<>();

        for (CategoryVo categoryItem : categoryList) {
//            param.remove("fields");
//            param.put("parent_id", categoryItem.getId());
            List<CategoryVo> categoryEntityList = categoryService.queryCategoryEntityList(categoryItem.getId());
            List<Integer> childCategoryIds = new ArrayList<>();
            for (CategoryVo categoryEntity : categoryEntityList) {
                childCategoryIds.add(categoryEntity.getId());
            }
            //
//            param = new HashMap<String, Object>();
//            param.put("categoryIds", childCategoryIds);
//            param.put("fields", "id as id, name as name, list_pic_url as list_pic_url, retail_price as retail_price");
//            //中大团队扩展
//            param.put("access_user_grade",grade);
//            param.put("root_dept_id", deptId);
//
//            PageHelper.startPage(0, 7, false);
            List<GoodsVo> categoryGoods = goodsService.queryCategoryList(deptId, grade, childCategoryIds);
            Map<String, Object> newCategory = new HashMap<String, Object>();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        return toResponsSuccess(resultObj);
    }

    /**
     * 中大团队扩展
     */
    @ApiOperation(value = "拼单商品信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
                        @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "collageGoods")
    public Object collageGoods(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("sale_type", "p");
//        param.put("is_delete", 0);
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("root_dept_id", deptId);
//        param.put("offset", 0);
//
//        param.put("fields", "id, name, list_pic_url, retail_price,collage_mini_num,collage_end_date,collage_curr_num, access_user_grade, client_price, vip_client_price, first_num, first_discount, second_num, second_discount");
//        PageHelper.startPage(0, 1, false);
        List<GoodsVo> collageGoods = goodsService.queryCollageGoodsList(deptId, grade, new Date(), 0, 7);
//        List<GoodsVo> collageGoodsList = new ArrayList<>();
        //中大团队扩展，过滤过期商品
//        Integer num = 0;
//        Date currDate = new Date();
//        for (GoodsVo goodsVo : collageGoods) {
//            if (goodsVo.getCollage_end_date().getTime() > currDate.getTime()) {
//                collageGoodsList.add(goodsVo);
//                num ++;
//            }
//            if (num == 7){
//                break;
//            }
//        }
        resultObj.put("collageGoodsList", collageGoods);

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "期货商品信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
            @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "futuresGoods")
    public Object futuresGoods(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("sale_type", "q");
//        param.put("is_delete", 0);
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("fields", "id, name, list_pic_url, retail_price, access_user_grade, client_price, vip_client_price, first_num, first_discount, second_num, second_discount");
//        param.put("root_dept_id", deptId);
//        param.put("offset", 0);
//        param.put("limit", 7);
//
//        PageHelper.startPage(0, 1, false);
        List<GoodsVo> futuresGoods = goodsService.queryFuturesGoodsList(deptId, grade, new Date(), 0, 7);
//        List<GoodsVo> futuresGoodsList = new ArrayList<>();
        //中大团队扩展，过滤过期商品
//        Integer num = 0;
//        Date currDate = new Date();
//        for (GoodsVo goodsVo :futuresGoods) {
//            if (goodsVo.getFutures_listing_date().getTime() > currDate.getTime()) {
//                futuresGoodsList.add(goodsVo);
//                num ++;
//            }
//            if (num == 7){
//                break;
//            }
//        }
        resultObj.put("futuresGoodsList", futuresGoods);

        return toResponsSuccess(resultObj);
    }

    /**
     * app首页
     */
    @ApiOperation(value = "新商品信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
            @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "newGoods")
    public Object newGoods(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("is_new", 1);
//        param.put("is_delete", 0);
//        param.put("fields", "id, name, list_pic_url, retail_price, sale_type, access_user_grade, client_price, vip_client_price, first_num, first_discount, second_num, second_discount, is_limited, is_allow_agent, price_type, counter_price, counter_sec_price, counter_thi_price, single_price, single_sec_price, single_thi_price");
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("root_dept_id", deptId);
//        param.put("offset", 0);
//        param.put("limit", 7);
//
//        PageHelper.startPage(0, 1, false);
        List<GoodsVo> newGoods = goodsService.queryNewGoodsList(deptId, grade, 0, 7);
        resultObj.put("newGoodsList", newGoods);
        //

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "新热门商品信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
            @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "hotGoods")
    public Object hotGoods(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
//        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("is_hot", "1");
//        param.put("is_delete", 0);
//        //中大团队扩展
//        param.put("access_user_grade",grade);
//        param.put("root_dept_id", deptId);
//        param.put("offset", 0);
//        param.put("limit", 7);
//
//        PageHelper.startPage(0, 1, false);
        List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(deptId, grade, 0,7);
        resultObj.put("hotGoodsList", hotGoods);
        //

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "topic")
    @IgnoreAuth
    @PostMapping(value = "topic")
    public Object topic() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("offset", 0);
//        param.put("limit", 3);
        List<TopicVo> topicList = topicService.queryTopicList();
        resultObj.put("topicList", topicList);
        //

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "brand")
    @IgnoreAuth
    @PostMapping(value = "brand")
    public Object brand(Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("is_new", 1);
//        param.put("sidx", "new_sort_order ");
//        param.put("order", "asc ");
//        param.put("offset", 0);
//        param.put("limit", 3);
        List<BrandVo> brandList = brandService.queryBrandList(deptId, 0, 3);
        resultObj.put("brandList", brandList);
        //

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "category")
    @ApiImplicitParams({@ApiImplicitParam(name = "deptId", value = "商家id", paramType = "path", required = true),
            @ApiImplicitParam(name = "grade", value = "用户等级", paramType = "path", required = true)})
    @PostMapping(value = "category")
    public Object category(Integer grade, Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();

        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("parent_id", 0);
//        param.put("sidx", "sort_order ");
//        param.put("order", "asc ");

        List<CategoryVo> categoryList = categoryService.queryCategoryList("sort_order ", "asc ");
        List<Map<String, Object>> newCategoryList = new ArrayList<>();

        for (CategoryVo categoryItem : categoryList) {
            param.put("parent_category_id", categoryItem.getId());
//            List<CategoryVo> categoryEntityList = categoryService.queryList(param);
//            List<Integer> childCategoryIds = null;
//            if (categoryEntityList != null && categoryEntityList.size() > 0) {
//                childCategoryIds = new ArrayList<>();
//                for (CategoryVo categoryEntity : categoryEntityList) {
//                    childCategoryIds.add(categoryEntity.getId());
//                }
//            }
            //
//            param = new HashMap<String, Object>();
//            param.put("categoryIds", childCategoryIds);
            //中大团队扩展
            param.put("access_user_grade", grade);
            param.put("root_dept_id", deptId);

//            param.put("fields", "");
            param.put("is_delete", "0");
            param.put("offset", 0);
            param.put("limit", 3);
            PageHelper.startPage(0, 7, false);
            List<GoodsVo> categoryGoods = goodsService.queryListCascade(param);
            Map<String, Object> newCategory = new HashMap<String, Object>();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        //

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "banner")
    @IgnoreAuth
    @PostMapping(value = "banner")
    public Object banner(Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();

//        Map<String, Object> param = new HashMap<String, Object>();
//
//        //中大团队扩展
//        param.put("root_dept_id", deptId);
//
//        //param.put("ad_position_id", 1);
        List<AdVo> banner = adService.queryBannerList(deptId);
        resultObj.put("banner", banner);

        return toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "channel")
    @IgnoreAuth
    @PostMapping(value = "channel")
    public Object channel(Integer deptId) {
        Map<String, Object> resultObj = new HashMap<String, Object>();

//        Map<String, Object> param = new HashMap<String, Object>();
//
//        //中大团队扩展
//        param.put("root_dept_id", deptId);
//        param.put("offset", 0);
//        param.put("limit", 5);
//
//        param.put("sidx", "sort_order ");
//        param.put("order", "asc ");
        List<ChannelVo> channel = channelService.queryChannelList(deptId, 0,5);
        resultObj.put("channel", channel);

        return toResponsSuccess(resultObj);
    }
}